package com.orange.core.service.mallArea.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.service.mallArea.MallAreaService;
import com.orange.core.mapper.floor.FloorMapper;
import com.orange.core.mapper.mallArea.MallAreaMapper;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.entity.floor.Floor;
import com.orange.core.pojo.entity.floor.vo.FloorVO;
import com.orange.core.pojo.entity.mallArea.MallArea;
import com.orange.core.pojo.entity.mallArea.dto.MallAreaDTO;
import com.orange.core.pojo.entity.mallArea.vo.MallAreaFloorVO;
import com.orange.core.pojo.entity.mallArea.vo.MallAreaVO;
import com.orange.core.pojo.req.mallArea.MallAreaPageReq;
import com.orange.core.util.ConvertUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>ClassName:MallAreaServiceImpl</p>
 * <p>Description: 商场区域service实现</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/24 16:53</p>
 */
@Service
@RequiredArgsConstructor
public class MallAreaServiceImpl extends ServiceImpl<MallAreaMapper, MallArea> implements MallAreaService {

    private final FloorMapper floorMapper;

    @Override
    public Page<MallAreaVO> page(PageReqDto<MallAreaPageReq> pageReqDto) {
        String areaName = Optional.ofNullable(pageReqDto.getCondition()).map(MallAreaPageReq::getAreaName).orElse(null);
        Page<MallArea> page = this.lambdaQuery().like(StrUtil.isNotBlank(areaName), MallArea::getAreaName, areaName)
                .orderByAsc(MallArea::getSortOrder)
                .page(pageReqDto.toPage());

        Page<MallAreaVO> mallAreaVOPage = new Page<>(pageReqDto.getPage(), pageReqDto.getSize(), page.getTotal());
        List<MallAreaVO> mallAreaVOList = page.getRecords().stream().map(mallArea -> {
            MallAreaVO mallAreaVO = new MallAreaVO();
            BeanUtil.copyProperties(mallArea, mallAreaVO);
            mallAreaVO.setAreaId(mallArea.getId());
            return mallAreaVO;
        }).collect(Collectors.toList());
        mallAreaVOPage.setRecords(mallAreaVOList);
        return mallAreaVOPage;
    }

    @Override
    public MallAreaVO info(Integer id) {
        MallArea mallArea = this.getById(id);
        Assert.notNull(mallArea,"商场区域不存在");
        MallAreaVO mallAreaVO = new MallAreaVO();
        mallAreaVO.setAreaId(mallArea.getId());
        BeanUtil.copyProperties(mallArea, mallAreaVO);
        return mallAreaVO;
    }

    @Override
    @Transactional
    public boolean save(MallAreaDTO mallAreaDTO) {
        MallArea mallArea = new MallArea();
        BeanUtil.copyProperties(mallAreaDTO, mallArea);
        boolean result = this.save(mallArea);
        return result;
    }

    @Override
    @Transactional
    public boolean update(MallAreaDTO mallAreaDTO) {
        MallArea mallArea = new MallArea();
        BeanUtil.copyProperties(mallAreaDTO, mallArea);
        boolean result = this.updateById(mallArea);
        return result;
    }

    @Override
    @Transactional
    public boolean deleteMallAreaByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }

    @Override
    public List<MallAreaVO> mallAreaList() {
        List<MallArea> mallAreaList = this.lambdaQuery().orderByAsc(MallArea::getSortOrder).list();
        List<MallAreaVO> mallAreaVOList = ConvertUtils.convertListToVo(mallAreaList, MallAreaVO.class);
        return mallAreaVOList;
    }

    @Override
    public List<MallAreaFloorVO> mallAreaFloorList() {
        //查询所有区域
        List<MallArea> areas = this.list();

        List<MallAreaFloorVO> areaFloorVOList = areas.stream()
                .map(area -> {
                    MallAreaFloorVO mallAreaFloorVO = new MallAreaFloorVO();
                    mallAreaFloorVO.setAreaId(area.getId());
                    mallAreaFloorVO.setAreaName(area.getAreaName());

                    //查询该区域下的所有楼层
                    LambdaQueryWrapper<Floor> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(Floor::getAreaId, area.getId())
                            .orderByAsc(Floor::getSortOrder);

                    List<Floor> floors = floorMapper.selectList(queryWrapper);

                    List<FloorVO> floorVOS = floors.stream().map(floor -> {
                        FloorVO floorVO = new FloorVO();
                        floorVO.setFloorId(floor.getId());
                        floorVO.setFloorName(floor.getFloorName());
                        return floorVO;
                    }).collect(Collectors.toList());
                    mallAreaFloorVO.setFloors(floorVOS);
                    return mallAreaFloorVO;
                }).collect(Collectors.toList());
        return areaFloorVOList;
    }
}
